<script setup lang="ts">
import HelloWorld from "../components/HelloWorld.vue";
import { ref, computed, watchEffect } from "vue";

import { ElCard ,ElButton, ElInput, ElFormItem } from "element-plus";
import axios from "axios";
const username = ref("");
const password = ref("");
const password2 = ref("");
const email = ref("");

const usernameError = ref("");
const passwordError = ref("");
const emailError = ref("");

const globalerror = ref("");
const isPassword2Valid = computed(() => {
  return password2.value == password.value;
});
const isUsernameValid = computed(() => {
  return username.value.length >= 3;
});

const isPasswordValid = computed(() => {
  const regexes = [
    /[a-z]/, // Lowercase letters
    /[A-Z]/, // Uppercase letters
    /[0-9]/, // Numbers
    /[!@#$%^&*(),.?":{}|<>]/ // Special characters
  ];

  const matches = regexes.filter(regex => regex.test(password.value));
  return password.value.length >= 6 && matches.length >= 2;
});
const isEmailValid = computed(() => {
  const regexes = [
    /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/ // Email format
  ];

  const matches = regexes.filter(regex => regex.test(email.value));
  return email.value.length >= 6 && matches.length >= 1;
});
function GetCookie(name:string) {
    const cookies = document.cookie.split(';');
    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim();
        if (cookie.startsWith(name + '=')) {
            return cookie.substring(name.length + 1);
        }
    }
    return '';
}
const Register = async () => {
  if(isEmailValid&&isUsernameValid&&isPasswordValid&&isPassword2Valid)
  {
    let csrftoken = GetCookie('csrftoken');
    console.log("Logging in with:", {
      username: username.value,
      password: password.value,
      email: email.value,
      csrftoken:csrftoken
    });
    const response = await axios.post("api/v2/register", {
      username: username.value,
      password: password.value,
      email: email.value,
    }, {
      headers: {
        'X-CSRFToken': csrftoken // replace 'token' with your token value
      }
    });
    if (response.data["state"] == "ok") {
      window.location.href = "/login";
    } else if (response.data["state"] == "error") {
      globalerror.value = response.data["error"];
    } else {
      globalerror.value = "未知错误";
    }
    console.log("Registration successful:", response.data);
  }
}

</script>

<template>
  <header><HelloWorld msg="欢迎登录Chats Next" /></header>
  <main>  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <Text><span style="font-size: 20pt;">Chats Next</span><br><span style="font-size: 17.5pt;">the most advance next generation of GPTs </span >
        </Text>
      </div>
    </template>
    <div class="login-container">
    <form @submit.prevent="Register">
      <div class="form-group">
        <label for="username">账号 : </label>
        <el-input
          size="large"
          type="text"
          id="username"
          v-model="username"
          placeholder="输入你的账号"
          required
        />
        <p v-if="!isUsernameValid&&username" class="error">用户名错误</p>
      </div>
      <div class="form-group">
        <label for="email">邮箱 : </label>
        <el-input
          size="large"
          type="email"
          id="email"
          v-model="email"
          placeholder="请验证你的密码"
          required
        />
        <p v-if="!isEmailValid&&email" class="error">邮箱格式错误</p>
      </div>
      <div class="form-group">
        <label for="password">密码 : </label>
        <el-input
          size="large"
          type="password"
          id="password"
          v-model="password"
          placeholder="输入你的密码，不小于6位"
          required
        />
      </div>
      <div class="form-group">
        <p v-if="!isPasswordValid&&password" class="error">密码错误</p>
        <label for="password2">密码 : </label>
        <el-input
          size="large"
          type="password"
          id="password2"
          v-model="password2"
          placeholder="请验证你的密码"
          required
        />
        <p v-if="password2&&!isPassword2Valid" class="error">两次输入密码不相同</p>
      </div>

      <el-button class="register-button" type="success" @click="Register" round>注册</el-button>
      <p v-if="globalerror.length>0" class="error">{{ globalerror }}</p>

    </form>
  </div>
  </el-card></main>


</template>

<style scoped>
header {
  line-height: 1.5;
  margin-top: rem;
  max-height: 100vh;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

.error {
    color: red;
    margin-top: 5px;
  }
/* 样式化登录表单 */
.login-container {
  max-width: 300px;
  margin: 0 auto;
}

form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 10px;
}

label {
  font-weight: bold;
}

input {
  padding: 5px;
  margin-top: 5px;
}

button {
  padding: 10px;
  background-color: #4caf50;
  color: #fff;
  border: none;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: center;
}
Text{
  color: #45a049;
}
label{
  color: #45a049;
}
.register-button {

background-color: #0091ff; /* Change to your preferred color */
color: white;
border: none;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
transition: background-color 0.3s ease;
}

.register-button:hover {
background-color: #0056b3; /* Darker shade for hover state */
}

.register-button:active {
background-color: #003d80; /* Even darker shade for active state */
}
@keyframes flyIn {
  0% {
    transform: translateX(-100%);
    opacity: 0;
  }
  100% {
    transform: translateX(0);
    opacity: 1;
  }
}

.box-card {
  animation: flyIn 1s ease-out;
  width: 300px;
  background-color:#00000010;
  border-width: 0;
  border-radius: 30px;
  display: block;
  margin: 0 auto 2rem;
}
.el-card {
  transition: transform 0.3s ease;

}

.el-card:hover {
  box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
  transform: scale(1.05);
}
@media (min-width: 1024px) {
  header {
    margin-top: 2rem;
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }
  .box-card {
  width: 480px;
  background-color:#00000010;
  border-width: 0;
  border-radius: 30px;
  display: block;
  margin: 0 auto 2rem;
}
}
</style>
